<?php

namespace App\Exports;

use App\Models\PhoneRecord;
use Maatwebsite\Excel\Concerns\Exportable;
use Illuminate\Contracts\Support\Responsable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
class PhoneRecordExport implements Responsable,FromQuery,WithMapping,WithHeadings,ShouldAutoSize
{
    use Exportable;

    private $fileName = 'phoneRecord.xlsx';

    protected  $name;
    protected  $username;
    protected  $true_name;
    protected  $start_recording_time;
    protected  $end_recording_time;
    protected  $created_at_time;
    protected  $updated_at_time;
    protected  $download;

    /**
     * @return \Illuminate\Support\Collection
     */
    public function query()
    {
        $phoneRecord = new PhoneRecord();
        $phoneRecords = $phoneRecord->search($this->name,$this->username,$this->true_name,$this->start_recording_time,$this->end_recording_time,$this->created_at_time,$this->updated_at_time,'',1,$this->download);

        return $phoneRecords;
    }


    public function condition($data){
        $this->name = isset($data['name'])?$data['name']:'';
        $this->username = isset($data['username'])?$data['username']:'';
        $this->true_name= isset($data['true_name'])?$data['true_name']:'';
        $this->start_recording_time = isset($data['start_recording_time'])?$data['start_recording_time']:'';
        $this->end_recording_time = isset($data['end_recording_time'])?$data['end_recording_time']:'';
        $this->created_at_time = isset($data['created_at_time'])?$data['created_at_time']:'';
        $this->updated_at_time = isset($data['updated_at_time'])?$data['updated_at_time']:'';
        $this->download = isset($data['download'])?$data['download']:'';

        $this->select = isset($data['select'])?$data['select']:'';
        return $this;
    }

    public function headings(): array
    {
        return [
            '电销人员账号',
            '拨出时间',
            '结束时间',
            '通话时长',
            '会员用户名',
            '会员姓名',
            '是否接通',
        ];
    }

    public function map($PhoneRecord): array
    {
        return [
            empty($PhoneRecord->user()->first()->name)?'':$PhoneRecord->user()->first()->name,
            $PhoneRecord->created_at,
            $PhoneRecord->updated_at,
            $PhoneRecord->recording_time,
            empty($PhoneRecord->fsUser()->first()->username)?'':$PhoneRecord->fsUser()->first()->username,
            empty($PhoneRecord->fsUser()->first()->true_name)?'':$PhoneRecord->fsUser()->first()->true_name,
            $PhoneRecord->call_status,
        ];
    }
}
